package com.xiaohe66.web.infrastructure.mybatis.task.convert;

import com.xiaohe66.common.util.IdWorker;
import com.xiaohe66.web.domain.common.value.TagId;
import com.xiaohe66.web.domain.task.agg.Task;
import com.xiaohe66.web.domain.task.value.TaskId;
import com.xiaohe66.web.infrastructure.domain.adapter.task.TaskConverter;
import com.xiaohe66.web.infrastructure.mybatis.common.model.TaskTagDo;
import com.xiaohe66.web.infrastructure.mybatis.common.repository.TagRepositoryImpl;
import com.xiaohe66.web.infrastructure.mybatis.task.model.TaskDo;
import com.xiaohe66.web.integration.DoConverter;
import org.mapstruct.Mapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;

/**
 * @author xiaohe
 * @since 2021.11.17 11:53
 */
@Mapper
public abstract class TaskDoConverter implements DoConverter<Task, TaskDo>, TaskConverter {

    @Autowired
    private TagRepositoryImpl tagRepository;

    public List<TaskTagDo> buildTaskTagOnlyValid(TaskId taskId, Set<TagId> tagIds) {
        return tagIds.stream()
                .filter(tagId -> tagRepository.isExist(tagId))
                .map(tagId -> buildTaskTagOnlyValid(taskId, tagId))
                .toList();
    }

    public TaskTagDo buildTaskTagOnlyValid(TaskId taskId, TagId tagId) {
        TaskTagDo taskTagDo = new TaskTagDo();
        taskTagDo.setId(IdWorker.genId());
        taskTagDo.setTagId(tagId.getValue());
        taskTagDo.setTaskId(taskId.getValue());
        return taskTagDo;
    }

}
